import {Utils} from '../../common/util'
import cookie from 'vue-cookies'
let utils = new Utils()
export const actions = {
  searchMovieList ({commit, state}) {
    utils.get('/search/' + state.searchText + '/' + state.currentPage).then(res => {
      commit('MOVIE_LIST', {movieList: res})
      commit('SEARCH_LOADING', {loading: false})
    })
  },
  login ({commit, state}) {
    utils.get('/login/' + state.user.name + '/' + state.user.passwd).then(res => {
      // 设置cookie 以及token
      cookie.set('hello', 'world')
    })
  },
  registerUser ({commit, state}) {
    utils.get('/registerUser/' + state.user.name + '/' + state.user.passwd).then(res => {
      // 设置各种
    })
  },
  changePassword ({commit, state}) {
    utils.get('/changePassword/' + state.user.name + '/' + state.user.passwd).then(res => {
      // 设置各种
    })
  },
  collectionMovie ({commit, state}) {
    utils.get('/collectionMovie/' + state.tid + '/' + state.mid)
  },
  deleteCollectionMovie ({commit, state}) {
    utils.get('/deleteCollectionMovie/' + state.tid + '/' + state.mid).then(res => {
      // 设置各种
    })
  },
  showCollectionMovie ({commit, state}) {
    console.log(state.tid)
    utils.get('/showCollectionMovie/' + state.tid).then(res => {
      commit('FAVOURITE_LIST', {favouriteList: res})
    })
  },
  deleteMovie ({commit, state}) {
    utils.get('/deleteMovie/' + state.mid)
  },
  findAllMovies ({commit, state}) {
    utils.get('/findAllMovies/' + state.cpage).then(res => {
      commit('LIST_COUNT', {listCount: res.countMovie})
      commit('MOVIE_LIST', {movieList: res.movie})
    })
  },
  deleteUser ({commit, state}) {
    utils.get('/deleteUser/' + state.tid)
  },
  updateUser ({commit, state}) {
    utils.post('/updateUser', state.user)
  },
  findAllUsers ({commit, state}) {
    // let url = '/findAllUsers'
    utils.get('/findAllUsers').then(res => {
      commit('USER_LIST', {userList: res})
    })
  },
  showHistoryList ({commit, state}) {
    // utils.get('/showHistoryList').then(res => {})
  },
  showFavouriteList ({commit, state}) {
    // utils.get('/showFavouriteList').then(res => {})
  },
  showMovieDetail ({commit, state}) {
    console.log('movie detail..', state.mid)
    utils.get('/detail/' + state.tid + '/' + state.mid).then(res => {
      console.log('action ..')
      console.log(res.ifCOllection)
      console.log(res.movieDetail)
      commit('IMOVIE', {imovie: JSON.parse(res.movieDetail)})
      commit('ISC', {isc: res.ifCOllection})
    })
  },
  editMovie ({commit, state}) {
    console.log(state.imovie)
    utils.post('/updateMovie', state.imovie)
  },
  findMovieByName ({commit, state}) {
    utils.get('/findMovieByName/' + state.searchText).then(res => {
      commit('MOVIE_LIST', {movieList: res})
    })
  }
}
